package yaty.lee.tencent.weibo.util;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.TreeSet;

import org.apache.log4j.Logger;

public class ReflectUtil {
    private static Logger log = Logger.getLogger(ReflectUtil.class);

    public static Map<String, Object> getAllField(Object object) {
        Map<String, Object> fieldMap = new LinkedHashMap<String, Object>();
        Field[] fields = null;

        Stack<Class<?>> classes = getClass(object);

        for (Class<?> clazz : classes) {
            try {
                fields = clazz.getDeclaredFields();
                for (Field field : fields) {
                    if (!field.isAccessible()) {
                        field.setAccessible(true);
                    }
                    // System.out.println(field.getName());
                    fieldMap.put(field.getName(), field.get(object));
                }
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        // System.out.println(fieldMap.size());

        return fieldMap;
    }

    public static Stack<Class<?>> getClass(Object object) {
        Stack<Class<?>> classes = new Stack<Class<?>>();
        Class<?> clazz = object.getClass();

        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
            classes.push(clazz);
        }

        return classes;
    }

    public static String getKeyValuePairs(Object object) {
        Set<String> keyValuePairs = new TreeSet<String>();
        Class<?> clazz = object.getClass();
        Field[] fields = null;

        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
            fields = clazz.getDeclaredFields();

            for (Field field : fields) {
                try {
                    if (!field.isAccessible()) {
                        field.setAccessible(true);
                    }

                    if ("url".equalsIgnoreCase(field.getName())) {
                        continue;
                    }
                    keyValuePairs.add(field.getName() + "=" + (field.get(object) == null ? "NULL" : field.get(object)));
                } catch (Exception e) {
                    log.error("反射" + clazz.getName() + "的字段" + field.getName() + "失败!", e);
                }
            }
        }

        return join(keyValuePairs, "&");
    }

    public static String join(Collection<String> collection, String delimiter) {
        if (collection == null || collection.isEmpty()) {
            return "";
        }

        Iterator<String> iterator = collection.iterator();
        StringBuffer sb = new StringBuffer(iterator.next());

        while (iterator.hasNext()) {
            sb.append(delimiter).append(iterator.next());
        }

        return sb.toString();
    }
}
